package com.yh.jandan.controller ;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.yh.jandan.ext.Init;
import com.yh.jandan.ext.LocalApp;
import com.yh.jandan.interceptor.SecureInterceptor;
import com.yh.jandan.model.Config;
import com.yh.jandan.model.Meizi;

public class ConfigController extends Controller {

	@Before( SecureInterceptor.class )
	public void index ( ) {
		List<Config> config = Config.dao.find ( "select * from config" ) ;
		for ( Config c : config ) {
			setAttr ( c.getName ( ) , c.getVal ( ) ) ;
		}
		setAttr ( "config" , config ) ;
		render ( "/config.jsp" ) ;
		return ;
	}

	public void updateConfig ( ) {
		try {
			Init.initData ( ) ;
			Meizi.init_ok_url_list ( ) ;
			renderText ( "0" ) ;
		} catch ( Exception e ) {
			renderText ( "1" ) ;
		}

	}

	@Before( SecureInterceptor.class )
	public void update ( ) {
		String p = getPara ( "p" , "0" ) ; // /分页大小
		String c = getPara ( "c" , "0" ) ; // 抓取页数
		String pswd = getPara ( "pswd" , "" ) ; // pswd
		String website = getPara ( "website" , "" ) ; // pswd
		String autocatch = getPara ( "autocatch" , "true" ) ; // pswd
		int pp = 0 ;
		int cc = 0 ;
		Map<String, String> msg = new HashMap<String, String> ( ) ;
		msg.put ( "retcode" , "-1" ) ;
		try {
			pp = Integer.parseInt ( p.trim ( ) ) ;
			cc = Integer.parseInt ( c.trim ( ) ) ;
		} catch ( NumberFormatException e ) {
			msg.put ( "retmsg" , "必须是大于0的数字" ) ;
			renderJson ( msg ) ;
			return ;
		}
		if ( pp < 1 || cc < 1 ) {
			msg.put ( "retmsg" , "必须是大于0的数字" ) ;
			renderJson ( msg ) ;
			return ;
		}
		if ( pp > 100 || cc > 10 ) {
			msg.put ( "retmsg" , "数字太大" ) ;
			renderJson ( msg ) ;
			return ;
		}
		Map<String, String> kkk = new HashMap<String, String> ( ) ;
		kkk.put ( "psize" , pp + "" ) ;
		kkk.put ( "csize" , cc + "" ) ;
		kkk.put ( "pswd" , pswd.trim ( ) ) ;
		kkk.put ( "website" , website.trim ( ) ) ;
		kkk.put ( "autocatch" , autocatch.trim ( ) ) ;
		// /
		for ( String name : kkk.keySet ( ) ) {
			String val = kkk.get ( name ) ;
			Config pconfig = Config.dao.findFirst ( "select * from config where name=?" , name ) ;

			if ( null == pconfig ) {
				pconfig = new Config ( ) ;
				pconfig.setName ( name ) ;
				pconfig.setVal ( val ) ;
				pconfig.setNote ( "[save]" ) ;
				pconfig.setCreatedate ( new Date ( ) ) ;
				pconfig.save ( ) ;
			} else {
				pconfig.setVal ( val ) ;
				pconfig.setNote ( "[update]" ) ;
				pconfig.setCreatedate ( new Date ( ) ) ;
				pconfig.update ( ) ;
			}
		}
		msg.put ( "retcode" , "0" ) ;
		renderJson ( msg ) ;

		updateData ( ) ;
		LocalApp.autocatch ( ) ;
		return ;
	}

	private void updateData ( ) {
		try {
			Init.initData ( ) ;
			Meizi.init_ok_url_list ( ) ;
		} catch ( Exception e ) {
			e.printStackTrace ( ) ;
		}
	}

	/*
	 * private String to_date ( Date date ) { return DateUtils.getDateByFormat (
	 * date , DateUtils.YMDHMS_FORMAT ); }
	 */

}
